//
//  ShippingView.m
//  YiShangPin
//
//  Created by 衣尚品1 on 15/3/16.
//  Copyright (c) 2015年 yishangpin2. All rights reserved.
//

#import "ShippingView.h"
#import "AppDelegate.h"

@implementation ShippingView{
    AppDelegate *delegate;
    CGContextRef context;
    int buttonY;
    float h1;
    float h2;
    float h3;
    float h4;
    float h5;
    UIFont *lableFont;
}



// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    //创建画布
    context = UIGraphicsGetCurrentContext();
    //设置线条颜色
    [[UIColor grayColor] set];
    //设置线条宽度
    CGContextSetLineWidth(context, 0.3f);
    
    delegate.isHaveHeight = NO;
    [self addView5];
    
    CGPoint newPoints[] = {
        CGPointMake(delegate.width/3, 30),
        CGPointMake(delegate.width/3, h5+20),
    };
    CGContextAddLines(context, newPoints, 2);
    
    CGPoint newPoints1[] = {
        CGPointMake(20, h1+10),
        CGPointMake(delegate.width-20, h1+10),
    };
    CGContextAddLines(context, newPoints1, 2);
    
    CGPoint newPoints2[] = {
        CGPointMake(20, h2+10),
        CGPointMake(delegate.width-20, h2+10),
    };
    CGContextAddLines(context, newPoints2, 2);
    
    CGPoint newPoints3[] = {
        CGPointMake(20, h3+10),
        CGPointMake(delegate.width-20, h3+10),
    };
    CGContextAddLines(context, newPoints3, 2);
    
    CGPoint newPoints4[] = {
        CGPointMake(20, h4+10),
        CGPointMake(delegate.width-20, h4+10),
    };
    CGContextAddLines(context, newPoints4, 2);
    
    CGContextStrokePath(context);
}

- (float)addView5{
    
    lableFont = [UIFont fontWithName:@"TimesNewRomanPS-ItalicMT" size:16];
    
    delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 0, 20, 20)];
    image.image = [UIImage imageNamed:@"shippingDetails.png"];
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 150, 18)];
    label1.text = @"Shipping Details";
    label1.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldItalicMT" size:18];
    //    label1.font = [UIFont boldSystemFontOfSize:16.0f];
    
    h1 = [self addLabel_str1:@"Estimated Shipping" str2:@"¥44 " height:0 Y:20];
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(delegate.width/3+10, 60, 80, 15)];
    label2.font = lableFont;
    label2.text = @"to China";
    
    h2 = [self addLabel_str1:@"Availability" str2:@"Ships to China 63 other countries" height:h1 Y:0];
    h3 = [self addLabel_str1:@"Estimated Awival" str2:@"4-10 days" height:h2 Y:0];
    h4 = [self addLabel_str1:@"Ships From" str2:@"China" height:h3 Y:0];
    h5 = [self addLabel_str1:@"Return Policy" str2:@"You may return all products within 30 days of delivay" height:0 Y:h4];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(delegate.width-80, h5+5, 80, 18)];
    [button setTitle:@"Details..." forState:0];
    button.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    [button setTitleColor:[UIColor colorWithRed:47.f/255 green:164.f/255 blue:230.f/255 alpha:1] forState:0];
    //button点击事件
    [button addTarget:self action:@selector(clickReturnPolicyDetails) forControlEvents:UIControlEventTouchUpInside];
    
    if (delegate.isHaveHeight == NO) {
        [self addSubview:image];
        [self addSubview:label1];
        [self addSubview:label2];
        [self addSubview:button];
    }
    
    return image.frame.size.height+h5+10;
}

- (double)addLabel_str1:(NSString *)str1 str2:(NSString *)str2 height:(double)height Y:(int)Y{
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(30, 30, 185, 50)];
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(30, 30, 185, 50)];
    label1.font = lableFont;
    label2.font = lableFont;
    label1.text = str1;
    label2.text = str2;
    
    label1.numberOfLines = 0;
    label2.numberOfLines = 0;
    label1.lineBreakMode = NSLineBreakByWordWrapping;
    label2.lineBreakMode = NSLineBreakByWordWrapping;
    
    CGSize size1 = [label1.text boundingRectWithSize:CGSizeMake(delegate.width/3-40, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:label1.font,NSFontAttributeName, nil] context:nil].size;
    CGSize size2 = [label2.text boundingRectWithSize:CGSizeMake(delegate.width/3*2-40, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:label2.font,NSFontAttributeName, nil] context:nil].size;
    
    label1.frame = CGRectMake(30, 20+height+Y, delegate.width/3-40, size1.height);
    label2.frame = CGRectMake(delegate.width/3+10, 20+height+Y, delegate.width/3*2-40, size2.height);
    
    //        lable1.backgroundColor = [UIColor redColor];
    
    
    if (delegate.isHaveHeight == NO) {
        [self addSubview:label1];
        [self addSubview:label2];
    }
    
    
    double height1 = (size1.height > size2.height) ? size1.height : size2.height;
    //    NSLog(@"label2:%f",delegate.width/3*2-40);
    
    return height1 + 20+height+Y;
}


- (void)clickReturnPolicyDetails{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ReturnPolicy" object:@"ReturnPolicyDetails"];
}

@end
